﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 首页DAL
    /// </summary>
    public class PrvFunctList
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public PrvFunctList()
        {

        }

        /// <summary>
        /// 根据登录ID获取相关菜单
        /// <creator>zhongyl</creator>
        /// <date>2013-07-02</date>
        /// </summary>
        /// <param name="loginID">用户登录ID</param>
        /// <returns>返回有权限访问的菜单</returns>
        public List<Model.PrvFunctList> GetPrvFunctList(string loginID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FFunctNo,FPreFunctNo,FfunctName,FfunctURL,FfunctType,Fnote,");
            strSql.Append("FFunctAlias,FPictName,FPictSelected,Fsort,fvisible,FfunctFullname,FIsNewWindow,FFunctSynonymsNo");
            strSql.Append(" FROM t_PrvFunctList");
            strSql.Append(" WHERE fvisible='Y' AND FFunctNo IN (");
            strSql.Append(" SELECT  DISTINCT B.FFunctNo FROM t_PrvUser A INNER JOIN (SELECT b.FLogID,c.FFunctNo FROM dbo.t_PrvUserGroup b ");
            strSql.Append(" INNER JOIN t_PrvGroupPriv c ON b.FGroupNo=c.FGroupNo) B ON A.FLogID=B.FLogID AND A.FLogID=@FLogID");
            strSql.Append(" )");
            SqlParameter[] parameters =  { 
                new SqlParameter("@FLogID",SqlDbType.VarChar,50)
            };
            parameters[0].Value = loginID;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return DataTableToList(ds.Tables[0]);

        }
        /// <summary>
        /// 获取所有相关菜单
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <returns>全部权限菜单</returns>
        public List<Model.PrvFunctList> GetPrvFunctList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FFunctNo,FPreFunctNo,FfunctName,FfunctURL,FfunctType,Fnote,");
            strSql.Append("FFunctAlias,FPictName,FPictSelected,Fsort,fvisible,FfunctFullname,FIsNewWindow,FFunctSynonymsNo");
            strSql.Append(" FROM t_PrvFunctList WHERE fvisible='Y'");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="dt">需转换数据源</param>
        /// <returns>转换后数据列表</returns>
        private List<Model.PrvFunctList> DataTableToList(DataTable dt)
        {
            List<Model.PrvFunctList> modelList = new List<Model.PrvFunctList>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.PrvFunctList model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.PrvFunctList();
                    model.FFunctNo = dt.Rows[n]["FFunctNo"].ToString();
                    model.FPreFunctNo = dt.Rows[n]["FPreFunctNo"].ToString();
                    model.FfunctName = dt.Rows[n]["FfunctName"].ToString();
                    model.FfunctURL = dt.Rows[n]["FfunctURL"].ToString();
                    model.FfunctType = dt.Rows[n]["FfunctType"].ToString();
                    model.Fnote = dt.Rows[n]["Fnote"].ToString();
                    model.FFunctAlias = dt.Rows[n]["FFunctAlias"].ToString();
                    model.FPictName = dt.Rows[n]["FPictName"].ToString();
                    model.FPictSelected = dt.Rows[n]["FPictSelected"].ToString();
                    if (dt.Rows[n]["Fsort"].ToString() != "")
                    {
                        model.Fsort = int.Parse(dt.Rows[n]["Fsort"].ToString());
                    }
                    model.Fvisible = dt.Rows[n]["fvisible"].ToString();
                    model.FfunctFullname = dt.Rows[n]["FfunctFullname"].ToString();
                    model.FIsNewWindow = dt.Rows[n]["FIsNewWindow"].ToString();
                    model.FFunctSynonymsNo = dt.Rows[n]["FFunctSynonymsNo"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }
    }
}
